package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentDscPaperPersonMapper;
import com.zhijian.medical.entity.dto.DscScoreDto;
import com.zhijian.medical.entity.po.AssessmentDscPaperPerson;
import com.zhijian.medical.entity.vo.request.AssessmentDscPaperPersonSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentDscPaperPersonVo;
import com.zhijian.medical.service.AssessmentDscPaperPersonService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-数字切片考试提交结果业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-02-03 22:09:21
 */
@Slf4j
@Service
public class AssessmentDscPaperPersonServiceImpl implements AssessmentDscPaperPersonService {

    @Autowired
    private AssessmentDscPaperPersonMapper mapper;

    @Override
    public AssessmentDscPaperPerson selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentDscPaperPersonVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByAssessmentId(Long assessmentId) {
        mapper.deleteByAssessmentId(assessmentId);
    }

    @Override
    public Long insert(AssessmentDscPaperPerson assessmentDscPaperPerson) {
        Date now = new Date();
        assessmentDscPaperPerson.setCreateTime(now);
        assessmentDscPaperPerson.setUpdateTime(now);
        return mapper.insert(assessmentDscPaperPerson) > 0 ? assessmentDscPaperPerson.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentDscPaperPerson assessmentDscPaperPerson) {
        Date now = new Date();
        assessmentDscPaperPerson.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentDscPaperPerson) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentDscPaperPerson record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AssessmentDscPaperPersonSearchVo assessmentDscPaperPersonSearchVo) {
        return mapper.count(assessmentDscPaperPersonSearchVo);
    }

    @Override
    public List<AssessmentDscPaperPersonVo> selectList(AssessmentDscPaperPersonSearchVo assessmentDscPaperPersonSearchVo) {
        return mapper.selectList(assessmentDscPaperPersonSearchVo);
    }

    @Override
    public AssessmentDscPaperPersonVo selectByAssessmentIdAndPersonIdAndCaseId(Long assessmentId, Long personId, Long caseId) {
        List<AssessmentDscPaperPersonVo> assessmentDscPaperPersonVos = mapper.selectList(AssessmentDscPaperPersonSearchVo.builder()
                .assessmentId(assessmentId)
                .userId(personId)
                .caseId(caseId)
                .build());
        return CollectionUtil.isNotEmpty(assessmentDscPaperPersonVos) ? assessmentDscPaperPersonVos.get(0) : null;
    }

    @Override
    public Page<AssessmentDscPaperPersonVo> selectPage(AssessmentDscPaperPersonSearchVo assessmentDscPaperPersonSearchVo) {
        //先直接查总数
        int count = count(assessmentDscPaperPersonSearchVo);
        //分页
        Page<AssessmentDscPaperPersonVo> page = new Page<>(true, count, assessmentDscPaperPersonSearchVo.getPageNum(), assessmentDscPaperPersonSearchVo.getPageSize());
        assessmentDscPaperPersonSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentDscPaperPersonVo> responseVos = this.selectList(assessmentDscPaperPersonSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentDscPaperPerson> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public boolean score(DscScoreDto dscScoreDto) {
        return mapper.score(dscScoreDto) != 0;
    }

}